package com.atguigu.gulimail.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.member.entity.UserAddressEntity;
import com.atguigu.gulimail.member.service.UserAddressService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 收货地址表
 *
 * @author guangxiaozhao
 * @email 1764773283@qq.com
 * @date 2021-01-21 09:25:51
 */
@RestController
@RequestMapping("member/useraddress")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:useraddress:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:useraddress:info")
    public R info(@PathVariable("id") Long id) {
        UserAddressEntity userAddress = userAddressService.getById(id);

        return R.ok().put("userAddress", userAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:useraddress:save")
    public R save(@RequestBody UserAddressEntity userAddress) {
        userAddressService.save(userAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:useraddress:update")
    public R update(@RequestBody UserAddressEntity userAddress) {
        userAddressService.updateById(userAddress);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:useraddress:delete")
    public R delete(@RequestBody Long[] ids) {
        userAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 根据用户id 查询出全部的用户收货地址
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public R userAddressByUserId(@PathVariable("userId") Long userId) {
        List<UserAddressEntity> userAddressEntities = userAddressService.listUserAddressByUserId(userId);
        return R.ok().put("data",userAddressEntities);
    }

}
